package com.jt.www.util;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jt.www.model.woxuebao.Risk.*;
import com.jt.www.model.woxuebao.toubao.ContextDuty;
import com.jt.www.model.woxuebao.toubao.ContextDutyLimit;
import com.jt.www.model.woxuebao.toubao.ContextRisk;

import java.util.List;
import java.util.Map;

/**
 * @author xq.wu03
 * @create 2019-05-17 20:01
 */
public class ChangeUtils {


    public static Map<String, ContextRisk> changeRisk(List<RiskDTO> riskList) {

        Map<String, ContextRisk> retMap = Maps.newHashMap();
        for (RiskDTO risk : riskList) {
            for (RiskLiability liability : risk.getLiabilityList()) {
                for (RiskLimit riskLimit : liability.getLimitList()) {
                    for (RiskLimitValue limitValue : riskLimit.getLimitValues()) {
                        //险种对象
                        ContextRisk contextRisk = new ContextRisk();
//                        contextRisk.setRiskTypeCode(risk.getCode());
//                        contextRisk.setRiskTypeName(risk.getName());
                        contextRisk.setRiskFlag2(risk.getRiskFlag());

                        //责任对象
                        ContextDuty dutie = new ContextDuty();
                        dutie.setDutyCode(liability.getInsuranceLiabilityCode());
                        dutie.setDutyName(liability.getInsuranceLiabilityName());

                        //险种限额对象
                        ContextDutyLimit dutyLimit = new ContextDutyLimit();
                        dutyLimit.setCode(riskLimit.getLiabilityLimitCode());
                        dutyLimit.setName(riskLimit.getLiabilityLimitName());
                        //险种限额值
                        dutyLimit.setValue(limitValue.getLiabilityLimitValues());
                        dutyLimit.setValueCode(limitValue.getLiabilityLimitValuesCode());

                        List<ContextDutyLimit> dutyLimits = Lists.newArrayList(dutyLimit);
                        dutie.setDutyLimits(dutyLimits);

                        List<ContextDuty> duties = Lists.newArrayList(dutie);
                        contextRisk.setDuties(duties);

                        retMap.put(limitValue.getLiabilityLimitValuesCode(), contextRisk);
                    }
                }
            }
        }
        return retMap;
    }

    public static void setLimitValue(ContextRisk contextRisk, LimitValue limitValue) {
        for (ContextDuty contextDuty : contextRisk.getDuties()) {
            if (contextDuty.getDutyCode().equals(limitValue.getDutieCode())) {
                for (ContextDutyLimit contextDutyLimit : contextDuty.getDutyLimits()) {
                    if (contextDutyLimit.getCode().equals(limitValue.getDutyLimitCode())) {
                        contextDutyLimit.setValue(limitValue.getValue());
                        contextDutyLimit.setValueCode(limitValue.getValueCode());
                    }
                }
            }
        }
    }

    public static ContextRiskMap toContextRiskMap(List<RiskDTO> riskList) {
        Map<String, ContextRisk> contextRisks = Maps.newHashMap();
        Map<String, LimitValue> dutyLimitValues = Maps.newHashMap();

        //得到主险
        String mainRiskCode = "";
        for (RiskDTO risk : riskList) {
            if ("M".equals(risk.getRiskFlag())) {
                mainRiskCode = risk.getCode();
                break;
            }
        }

        for (RiskDTO risk : riskList) {
            //险种对象
            ContextRisk contextRisk = new ContextRisk();
            contextRisk.setRiskCode(risk.getCode());
            contextRisk.setRiskFlag2(risk.getRiskFlag());
            contextRisk.setRiskTypeName(risk.getClassName());
            contextRisk.setKindName(risk.getName());
            contextRisk.setMainRiskCode(mainRiskCode);
            List<ContextDuty> duties = Lists.newArrayList();
            for (RiskLiability liability : risk.getLiabilityList()) {
                //责任对象
                ContextDuty dutie = new ContextDuty();
                dutie.setDutyCode(liability.getInsuranceLiabilityCode());
                dutie.setDutyName(liability.getInsuranceLiabilityName());
                List<ContextDutyLimit> dutyLimits = Lists.newArrayList();
                for (RiskLimit riskLimit : liability.getLimitList()) {
                    //险种限额对象
                    ContextDutyLimit dutyLimit = new ContextDutyLimit();
                    dutyLimit.setCode(riskLimit.getLiabilityLimitCode());
                    dutyLimit.setName(riskLimit.getLiabilityLimitName());
                    dutyLimits.add(dutyLimit);
                    for (RiskLimitValue limitValue : riskLimit.getLimitValues()) {
                        //险种限额值
                        LimitValue LimitValue = new LimitValue();
                        LimitValue.setValue(limitValue.getLiabilityLimitValues());
                        LimitValue.setValueCode(limitValue.getLiabilityLimitValuesCode());
                        LimitValue.setDutyLimitCode(dutyLimit.getCode());
                        LimitValue.setDutieCode(dutie.getDutyCode());

                        dutyLimitValues.put(limitValue.getLiabilityLimitValuesCode(), LimitValue);
                    }
                }
                dutie.setDutyLimits(dutyLimits);
                duties.add(dutie);
            }
            contextRisk.setDuties(duties);
            contextRisks.put(contextRisk.getRiskCode(), contextRisk);
        }

        return new ContextRiskMap(contextRisks, dutyLimitValues);
    }

}
